<?php

/*
 * litchicms
 * Copyright (C) 2010-7-19 Author: tiefa(539247@qq.com)
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
!defined('BRK') && exit("!");

if (!function_exists('Component_ui_titleimg')) {

    function Component_ui_titleimg($arr=FALSE) {
        !$arr && exit('Component_ui_titleimg');
        $input = input_text_control('titleimg', $arr['collection_value']['titleimgid'], 'titleimg_element', 'text', 'input_text');
        if ($arr['collection_value']['titleimgid'] != 0) {
            $img = "<img src='" . $arr['collection_value']['titleimg'] . "' />";
        } else {
            $img = "";
        }

        return "
    <div class='content_group_row'>
        <table class='thetable' border='0' cellpadding='0' cellspacing='1'>
            <tr>
                <td class='td1' bgcolor='#ffffff'>
                    <div>
                        <div class='cname'><b>标题图片</b>:</div>
                        <div class='cdesc'>在附件里面选一张图片,如果没有请上传</div>
                    </div>
                </td>
                <td class='td2' >
                    <div>
                    <div class='hidden'>$input</div>
                    <div id=\"titleimgdiv\">$img</div>
                    </div>
                </td>
            </tr>
        </table>
    </div>
    ";
    }

}

$BRK_GLOBAL['ComponentValidationFunction'][] = 'Component_Validation_titleimg';

if (!function_exists('Component_Validation_titleimg')) {

    function Component_Validation_titleimg() {
        if (!empty($_POST['field']['titleimg'])) {
            $temp = $_POST['field']['titleimg'];
            $_POST['field']['titleimg'] = '';
            $_POST['field']['titleimgid'] = 0;
            $id = intval($temp);
            if ($id > 0) {
                if ($one = Component_attachment_action_rigth($id)) {
                    if ($one['attach_sys_suffix'] == '.png' || $one['attach_sys_suffix'] == '.jpg') {
                        if (is_file($one['attach_thumb'])) {
                            $_POST['field']['titleimgid'] = $id;
                            $_POST['field']['titleimg'] = $one['attach_thumb'];
                            return TRUE;
                        }
                    }
                }
            }
            return "标题图片不合法,如果没有附件,请先上传";
        }
        return "必须选择一个标题图片,如果没有附件,请先上传";
    }

}



if (!function_exists('Component_buildUiParam_titleimg')) {

    function Component_buildUiParam_titleimg($COLLECTION_ID=0, $ONE_DATA=NULL) {
        if (!$COLLECTION_ID || !$ONE_DATA) {
            return array('collection_value' => FALSE);
        }
        return array('collection_value' => $ONE_DATA);
    }

}



if (!function_exists('Component_buildField_titleimg')) {

    function Component_buildField_titleimg() {
        return array('titleimgid' => $_POST['field']['titleimgid'], 'titleimg' => $_POST['field']['titleimg']);
    }

}